package com.example.edianzu.somur.module.mine.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.edianzu.somur.App;
import com.example.edianzu.somur.R;
import com.example.edianzu.somur.api.constans.AppContents;
import com.example.edianzu.somur.api.manager.APIManager;
import com.example.edianzu.somur.module.mine.bean.RevisePassword;
import com.example.edianzu.somur.utils.content.StatusBarUtils;
import com.example.edianzu.somur.utils.view.BaseActivity;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public class RevisePasswordActivity extends BaseActivity {

    @BindView(R.id.activity_revise_oldPassword)
    AppCompatEditText activityReviseOldPassword;
    @BindView(R.id.activity_revise_newPassword)
    AppCompatEditText activityReviseNewPassword;
    @BindView(R.id.activity_personalcenter_alter)
    AppCompatButton activityPersonalcenterAlter;
    @BindView(R.id.activity_revise_back)
    AppCompatImageView activityReviseBack;

    private static final String TAG = "RevisePasswordActivity";

    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.setWindowStatusBarColor(this, R.color.title);
        setContentView(R.layout.activity_revise_password);
        ButterKnife.bind(this);

        sharedPreferences = App.getApp().getSharedPreferences();
    }

    @OnClick({R.id.activity_personalcenter_alter,R.id.activity_revise_back})
    public void onViewClicked(View view) {

        switch (view.getId()) {

            case R.id.activity_revise_back:

                finish();
                break;
            case R.id.activity_personalcenter_alter:
                final String oldPassword = activityReviseOldPassword.getText().toString();
                final String newPassword = activityReviseNewPassword.getText().toString();

                final int member_id = getIntent().getIntExtra("member_id", 0);
                Log.d(TAG, "onCreate: " + member_id);

                if (!TextUtils.isEmpty(oldPassword) && !TextUtils.isEmpty(newPassword)) {

                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("member_id", member_id + "");
                    hashMap.put("old_password", oldPassword);
                    hashMap.put("password", newPassword);

                    APIManager.getApiManagerInstance().setModifyPassword(new Observer<RevisePassword>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NonNull RevisePassword revisePassword) {

                            if (revisePassword.getStatus() == AppContents.STATUS) {

                                Toast.makeText(RevisePasswordActivity.this, "密码修改成功！！！", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {

                                Toast.makeText(RevisePasswordActivity.this, "密码修改失败，请重新修改！！！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    }, hashMap);
                } else {

                    Toast.makeText(RevisePasswordActivity.this, "密码为空，请重新输入！！！", Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
